package com.ruoyi.legal.controller;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.ResponseModel;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.Pages;
import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.legal.domain.Meeting;
import com.ruoyi.legal.service.IMeetingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
/**
 * 会议纪要Controller
 * 
 * @author xwj
 * @date 2025-05-27
 */
@RestController
@RequestMapping("/legal/meeting")
@Api(tags = "会议纪要")
public class MeetingController extends BaseController {
    @Autowired
    private IMeetingService meetingService;

    /**
     * 查询会议纪要列表
     */
    @PostMapping("/list")
    @ApiOperation("分页查询")
    public TableDataInfo list(@RequestBody Meeting meeting) {
        startPage();
        List<Meeting> list = meetingService.selectMeetingList(meeting);
        return getDataTable(list);
    }

    @PostMapping("/listAll")
    @ApiOperation("不分页查询")
    public AjaxResult listAll(@RequestBody Meeting meeting) {
        startOrderBy();
        List<Meeting> list = meetingService.selectMeetingList(meeting);
        return success(list);
    }

    /**
     * 导出会议纪要列表
     */
    @Log(title = "会议纪要", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, Meeting meeting) {
        List<Meeting> list = meetingService.selectMeetingList(meeting);
        ExcelUtil<Meeting> util = new ExcelUtil<Meeting>(Meeting.class);
        util.exportExcel(response, list, "会议纪要数据");
    }

    /**
     * 获取会议纪要详细信息
     */
    @GetMapping(value = "/info/{id}")
    @ApiOperation("详情")
    public AjaxResult info(@PathVariable("id") Long id)  {
        return success(meetingService.selectMeetingById(id));
    }

    /**
     * 新增会议纪要
     */
    @Log(title = "会议纪要", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    @ApiOperation("新增")
    public AjaxResult save(@RequestBody Meeting meeting) {
        SysUser user = getLoginUser().getUser();
        if(meeting.getId() == null) {
        meeting.setCreateId(user.getUserId());
            meeting.setCreateTime(DateUtils.getNowDate());
            meeting.setCreateBy(user.getNickName());
            meetingService.insertMeeting(meeting);
        } else {
            meeting.setUpdateId(user.getUserId());
            meeting.setUpdateTime(DateUtils.getNowDate());
            meeting.setUpdateBy(user.getNickName());
            meetingService.updateMeeting(meeting);
        }

        return success(meeting);
    }

    /**
     * 修改会议纪要
     */
    @Log(title = "会议纪要", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    @ApiOperation("修改")
    public AjaxResult update(@RequestBody Meeting meeting) {
        SysUser user = getLoginUser().getUser();
                meeting.setUpdateId(user.getUserId());
                meeting.setUpdateTime(DateUtils.getNowDate());
                meeting.setUpdateBy(user.getNickName());
        return success(meetingService.updateMeeting(meeting));
    }

    @PostMapping("/submit")
    @ApiOperation("提交")
    public AjaxResult submit(@RequestBody Meeting meeting) {
        SysUser user = getLoginUser().getUser();
        Date now = DateUtils.getNowDate();

        meeting.setStatus("1");
        meeting.setSubmitTime(now);
        meeting.setUpdateId(user.getUserId());
        meeting.setUpdateTime(now);
        meeting.setUpdateBy(user.getNickName());
        meetingService.updateMeeting(meeting);
        return success();
    }

    @PostMapping("/recall")
    @ApiOperation("撤回")
    public AjaxResult recall(@RequestBody Meeting meeting) {
        SysUser user = getLoginUser().getUser();

        meeting.setStatus("0");
        meeting.setSubmitTime(null);
        meeting.setUpdateId(user.getUserId());
        meeting.setUpdateTime(DateUtils.getNowDate());
        meeting.setUpdateBy(user.getNickName());
        meetingService.updateMeeting(meeting);
        return success();
    }

    @PostMapping("/examine")
    @ApiOperation("审核")
    public AjaxResult examine(@RequestBody Meeting meeting) {
        SysUser user = getLoginUser().getUser();
        String status = meeting.getStatus();

        Date now = DateUtils.getNowDate();

        // 审核通过
        if("2".equals(status)) {
            meeting.setReason("");
        }
        meeting.setAuditBy(user.getUserName());
        meeting.setAuditId(user.getUserId());
        meeting.setAuditTime(now);
        meeting.setUpdateId(user.getUserId());
        meeting.setUpdateTime(DateUtils.getNowDate());
        meeting.setUpdateBy(user.getNickName());

        meetingService.updateMeeting(meeting);

        return success();
    }

    /**
     * 删除会议纪要
     */
    @Log(title = "会议纪要", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除或批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(meetingService.deleteMeetingByIds(ids));
    }

    @Log(title = "会议纪要", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation("条件删除")
    public ResponseModel delete(@RequestBody Meeting meeting) {
        int num = meetingService.delete(meeting);
        return ResponseModel.of(num);
    }

    @PostMapping("/queryPage")
    @ApiOperation("分页查询-mp")
    public ResponseModel<Pages<Meeting>> queryPage(@RequestBody Meeting meeting) {
        Pages<Meeting> page = meetingService.queryPage(meeting);
        return ResponseModel.of(page);
    }

    @PostMapping("/download")
    @ApiOperation("导出会议纪要")
    public ResponseEntity<Resource> download(HttpServletResponse response, Meeting meeting) {
        List<Meeting> meetinglist = meetingService.selectMeetingList(meeting);

        String zipFile = meetingService.generTempFile(meetinglist);
        Resource resource = new FileSystemResource(zipFile);
        String fileName = "会议纪要导出";

        try {
            fileName = new String(fileName.getBytes("utf-8"),"ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        String contentType = "application/octet-stream";
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType(contentType))
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + fileName + "\"")
                .body(resource);
    }
}
